/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.headless.application;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import net.zarubsys.robotplayground.core.Environment;
import net.zarubsys.robotplayground.core.device.IDevice;
import net.zarubsys.robotplayground.core.modules.EmptyModuleConfiguration;
import net.zarubsys.robotplayground.core.modules.IModule;
import net.zarubsys.robotplayground.core.modules.IModuleConfiguration;
import net.zarubsys.robotplayground.core.monitors.IMonitor;
import net.zarubsys.robotplayground.core.program.API;
import net.zarubsys.robotplayground.core.program.IProgram;
import net.zarubsys.robotplayground.core.program.ProgramRunnable;
import net.zarubsys.robotplayground.dummydevice.DummyDevice;
import net.zarubsys.robotplayground.dummydevice.DummyDeviceConfiguration;
import net.zarubsys.robotplayground.dummydevice.modules.DummyMotionModule;
import net.zarubsys.robotplayground.dummydevice.modules.DummyTelemetryModule;
import net.zarubsys.robotplayground.dummydevice.monitors.DummyPositionMonitor;
import net.zarubsys.robotplayground.scriptprogram.AbstractScriptProgramConfiguration;
import net.zarubsys.robotplayground.scriptprogram.ScriptProgram;
import net.zarubsys.robotplayground.scriptprogram.StringScriptProgramConfiguration;

import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

/**
 * HeadlessApplication
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class HeadlessApplication implements IApplication {
	
	private static final String PROGRAM = 
			"define('data', 'y', dummyTelemetry.getY())\n" +
			"define('data', 'x', dummyTelemetry.getX())\n" +
			"dummyMotion.move(10)\n" +
			"dummyMotion.rotate(450)\n" + 
			"dummyMotion.move(-10)\n" +
			"define('data', 'y', dummyTelemetry.getY())\n" +
			"define('data', 'x', dummyTelemetry.getX())\n" +
			"dump()";

	@Override
	public Object start(IApplicationContext context) throws Exception {
		Environment env = new Environment(Platform.getInstanceLocation().getURL().getFile() + "sessionLog.log");
		
		DummyDeviceConfiguration ddCfg = new DummyDeviceConfiguration();
		DummyDevice dd = new DummyDevice();
		dd.init(ddCfg, env);
		
		DummyTelemetryModule tm = new DummyTelemetryModule();
		tm.init(dd, EmptyModuleConfiguration.getInstance() , env);
		
		DummyMotionModule mm = new DummyMotionModule();
		mm.init(dd, EmptyModuleConfiguration.getInstance(), env);
		
		Set<IModule<? extends IDevice, ? extends IModuleConfiguration>> modules = new HashSet<>();
		modules.add(tm);
		modules.add(mm);
		
		DummyPositionMonitor positionMonitor = new DummyPositionMonitor();
		API api = new API(modules, Collections.singleton((IMonitor) positionMonitor), env);
		
		AbstractScriptProgramConfiguration programCfg = new StringScriptProgramConfiguration(PROGRAM);
		IProgram program = new ScriptProgram();
		
		@SuppressWarnings("unchecked") // ... too much generics :)
		ProgramRunnable runnable = new ProgramRunnable(program, api, programCfg, env);
		runnable.run();
		
		return null;
	}

	@Override
	public void stop() {
		// ignored
	}

}

